package br.com.view;

import br.com.dao.ConectaBanco;
import br.com.dao.ModeloTabela;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;

/**
 *
 * @author Cícero
 */
public class Frm_EntregaEpi extends javax.swing.JFrame {

    ConectaBanco conexao = new ConectaBanco();
    int flag = 0;
    //Frm_Acesso frmAcesso = new Frm_Acesso();

    public Frm_EntregaEpi() {
        initComponents();

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblIdEntrega = new javax.swing.JLabel();
        lblData = new javax.swing.JLabel();
        lblHora = new javax.swing.JLabel();
        lblEntregaEpi = new javax.swing.JLabel();
        textIdEntrega = new javax.swing.JTextField();
        lblObservacoes = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        textObservacoes = new javax.swing.JTextPane();
        btnNovaEntrega = new javax.swing.JButton();
        btnFinalizarEntrega = new javax.swing.JButton();
        btnPMatricula = new javax.swing.JButton();
        textPesquisaMatricula = new javax.swing.JTextField();
        ftextData = new javax.swing.JFormattedTextField();
        ftextHora = new javax.swing.JFormattedTextField();
        lblPesquisaMatricula = new javax.swing.JLabel();
        textPesquisaEpi = new javax.swing.JTextField();
        btnPEPI = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        lblQuantidade = new javax.swing.JLabel();
        textQuantidade = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableItens = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        lblResponsavel = new javax.swing.JLabel();
        textResponsavel = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablePesquisa = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        textPesquisaNome = new javax.swing.JTextField();
        btnPNome = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        lblMostraCodProd = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SysEpi - Entrega de EPI");
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblIdEntrega.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblIdEntrega.setText("Nº da Entrega:");
        getContentPane().add(lblIdEntrega, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 80, 90, -1));

        lblData.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblData.setText("Data:");
        getContentPane().add(lblData, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 80, 40, -1));

        lblHora.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblHora.setText("Hora:");
        getContentPane().add(lblHora, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 80, 40, -1));

        lblEntregaEpi.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N
        lblEntregaEpi.setText("Entrega EPI");
        getContentPane().add(lblEntregaEpi, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 20, 150, -1));

        textIdEntrega.setEnabled(false);
        getContentPane().add(textIdEntrega, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 80, 100, -1));

        lblObservacoes.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblObservacoes.setText("Observações:");
        getContentPane().add(lblObservacoes, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 390, 80, 20));

        textObservacoes.setEnabled(false);
        jScrollPane1.setViewportView(textObservacoes);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 410, 210, 140));

        btnNovaEntrega.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon(42).png"))); // NOI18N
        btnNovaEntrega.setText("Nova Entrega");
        btnNovaEntrega.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovaEntregaActionPerformed(evt);
            }
        });
        getContentPane().add(btnNovaEntrega, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 610, -1, -1));

        btnFinalizarEntrega.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (20).png"))); // NOI18N
        btnFinalizarEntrega.setText("Finalizar Entrega");
        getContentPane().add(btnFinalizarEntrega, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 610, -1, -1));

        btnPMatricula.setText("Pesquise");
        btnPMatricula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPMatriculaActionPerformed(evt);
            }
        });
        getContentPane().add(btnPMatricula, new org.netbeans.lib.awtextra.AbsoluteConstraints(530, 130, -1, -1));

        textPesquisaMatricula.setEnabled(false);
        getContentPane().add(textPesquisaMatricula, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 130, 140, -1));

        try {
            ftextData.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftextData.setEnabled(false);
        getContentPane().add(ftextData, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 80, 100, -1));

        try {
            ftextHora.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##:##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        ftextHora.setEnabled(false);
        getContentPane().add(ftextHora, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 80, 70, -1));

        lblPesquisaMatricula.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblPesquisaMatricula.setText("Pesquisar Matricula Funcionario:");
        getContentPane().add(lblPesquisaMatricula, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 130, 190, -1));

        textPesquisaEpi.setEnabled(false);
        getContentPane().add(textPesquisaEpi, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 210, 200, -1));

        btnPEPI.setText("Pesquisa");
        btnPEPI.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPEPIActionPerformed(evt);
            }
        });
        getContentPane().add(btnPEPI, new org.netbeans.lib.awtextra.AbsoluteConstraints(380, 210, -1, -1));

        jLabel1.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel1.setText("Pesquise o nome do  EPI:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 210, 150, -1));

        lblQuantidade.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        lblQuantidade.setText("Quantidade:");
        getContentPane().add(lblQuantidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(620, 210, 80, -1));

        textQuantidade.setEnabled(false);
        getContentPane().add(textQuantidade, new org.netbeans.lib.awtextra.AbsoluteConstraints(700, 210, 80, -1));

        tableItens.setEnabled(false);
        jScrollPane2.setViewportView(tableItens);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 410, 440, 140));

        jLabel2.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
        jLabel2.setText("Itens Para Entrega:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 390, 140, -1));

        lblResponsavel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblResponsavel.setText("Responsável:");
        getContentPane().add(lblResponsavel, new org.netbeans.lib.awtextra.AbsoluteConstraints(570, 80, -1, -1));

        textResponsavel.setEnabled(false);
        getContentPane().add(textResponsavel, new org.netbeans.lib.awtextra.AbsoluteConstraints(660, 80, 110, -1));

        tablePesquisa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tablePesquisa.setEditingColumn(0);
        tablePesquisa.setEditingRow(0);
        tablePesquisa.setEnabled(false);
        tablePesquisa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablePesquisaMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tablePesquisa);

        getContentPane().add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 270, -1, 100));

        jSeparator1.setForeground(new java.awt.Color(0, 102, 102));
        getContentPane().add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, 790, -1));

        jSeparator2.setForeground(new java.awt.Color(0, 102, 102));
        getContentPane().add(jSeparator2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 250, 790, -1));

        jSeparator3.setForeground(new java.awt.Color(0, 102, 102));
        getContentPane().add(jSeparator3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 570, 790, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
        jLabel3.setText("Pesquisar Nome Funcionario:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(141, 170, 170, -1));

        textPesquisaNome.setEnabled(false);
        getContentPane().add(textPesquisaNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(310, 170, 240, -1));

        btnPNome.setText("Pesquise");
        btnPNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPNomeActionPerformed(evt);
            }
        });
        getContentPane().add(btnPNome, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 170, -1, -1));

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icones sysepi/favicon (3).png"))); // NOI18N
        jButton1.setText("Cancelar Entrega");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 610, 160, -1));

        lblMostraCodProd.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        getContentPane().add(lblMostraCodProd, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 210, 100, 20));

        setSize(new java.awt.Dimension(827, 716));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnNovaEntregaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovaEntregaActionPerformed
        ftextData.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date(System.currentTimeMillis())));
        ftextHora.setText(new SimpleDateFormat("hh:mm:ss").format(new Time(System.currentTimeMillis())));
        NovaEntrada();
    }//GEN-LAST:event_btnNovaEntregaActionPerformed

    private void btnPMatriculaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPMatriculaActionPerformed

        conexao.conexao();
        geraTabelaMatricula("select * from cadastrofuncionario where matricula_funcionario ='" + textPesquisaMatricula.getText() + "'");

    }//GEN-LAST:event_btnPMatriculaActionPerformed

    private void btnPNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPNomeActionPerformed

        conexao.conexao();
        geraTabelaNome("select * from cadastrofuncionario where nome_funcionario like '%" + textPesquisaNome.getText() + "%'");
        flag = 1;
    }//GEN-LAST:event_btnPNomeActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Cancelar();
        conexao.desconecta();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btnPEPIActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPEPIActionPerformed

        conexao.conexao();
        geraTabelaProduto("select * from cadastroepi where nome_epi like'%" + this.textPesquisaEpi.getText() + "%'");
    }//GEN-LAST:event_btnPEPIActionPerformed

    private void tablePesquisaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablePesquisaMouseClicked
        if (flag == 1) {
            String nomeFuncionario = "" + tablePesquisa.getValueAt(0, 0);
            textPesquisaNome.setText(nomeFuncionario);
        } else {
            conexao.conexao();
            try {                
                String nomeEpi = "" + tablePesquisa.getValueAt(0, 1);
                conexao.executaSQL("select * from cadastroepi where nome_epi='" + nomeEpi + "'");
                conexao.rs.first();
                String codProd = conexao.rs.getString("codprod_epi");
                textPesquisaEpi.setText(nomeEpi);
                lblMostraCodProd.setText(codProd);
                textQuantidade.setText("1");
                conexao.desconecta();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Erro ao Preencher CodProd" + ex);
                conexao.desconecta();
            }
        }
    }//GEN-LAST:event_tablePesquisaMouseClicked

    private void NovaEntrada() {
        textPesquisaEpi.setEnabled(true);
        textPesquisaMatricula.setEnabled(true);
        textObservacoes.setEnabled(true);
        textQuantidade.setEnabled(true);
        textPesquisaNome.setEnabled(true);
        textPesquisaMatricula.requestFocus();
        textResponsavel.setText("Admin");
    }

    private void Cancelar() {
        textPesquisaEpi.setEnabled(false);
        textPesquisaMatricula.setEnabled(false);
        textObservacoes.setEnabled(false);
        textQuantidade.setEnabled(false);
        textPesquisaNome.setEnabled(false);
        textResponsavel.setText("");
        textPesquisaEpi.setText("");
        textPesquisaMatricula.setText("");
        textObservacoes.setText("");
        textQuantidade.setText("");
        textPesquisaNome.setText("");
        ftextData.setText("");
        ftextHora.setText("");
        //tablePesquisa.

    }

    public void geraTabelaMatricula(String SQL) {
        ArrayList dados = new ArrayList();
        String[] Colunas = new String[]{"Matricula", "Nome"};
        conexao.executaSQL(SQL);
        try {
            conexao.rs.first();
            do {
                dados.add((new Object[]{conexao.rs.getString("matricula_funcionario"), conexao.rs.getString("nome_funcionario")}));
            } while (conexao.rs.next());
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher o arrayList" + ex);
            conexao.desconecta();
        }
        ModeloTabela modeloTabela = new ModeloTabela(dados, Colunas);
        tablePesquisa.setModel(modeloTabela);
        tablePesquisa.getColumnModel().getColumn(0).setPreferredWidth(70);
        tablePesquisa.getColumnModel().getColumn(0).setResizable(false);
        tablePesquisa.getColumnModel().getColumn(1).setPreferredWidth(200);
        tablePesquisa.getColumnModel().getColumn(1).setResizable(false);

        tablePesquisa.getTableHeader().setReorderingAllowed(false);
        tablePesquisa.setAutoResizeMode(tablePesquisa.AUTO_RESIZE_OFF);
        tablePesquisa.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        conexao.desconecta();
    }

    public void geraTabelaNome(String SQL) {
        ArrayList dados = new ArrayList();
        String[] Colunas = new String[]{"Nome", "Matricula"};
        conexao.executaSQL(SQL);
        try {
            conexao.rs.first();
            do {
                dados.add((new Object[]{conexao.rs.getString("nome_funcionario"), conexao.rs.getString("matricula_funcionario")}));
            } while (conexao.rs.next());
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher o arrayList" + ex);
            conexao.desconecta();
        }
        ModeloTabela modeloTabela = new ModeloTabela(dados, Colunas);
        tablePesquisa.setModel(modeloTabela);
        tablePesquisa.getColumnModel().getColumn(0).setPreferredWidth(200);
        tablePesquisa.getColumnModel().getColumn(0).setResizable(false);
        tablePesquisa.getColumnModel().getColumn(1).setPreferredWidth(70);
        tablePesquisa.getColumnModel().getColumn(1).setResizable(false);

        tablePesquisa.getTableHeader().setReorderingAllowed(false);
        tablePesquisa.setAutoResizeMode(tablePesquisa.AUTO_RESIZE_OFF);
        tablePesquisa.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        conexao.desconecta();
    }

    public void geraTabelaProduto(String SQL) {
        ArrayList dados = new ArrayList();
        String[] Colunas = new String[]{"Codigo Estoque", "Nome", "Quantidade", "Tamanho"};
        conexao.executaSQL(SQL);
        try {
            conexao.rs.first();
            do {
                dados.add((new Object[]{conexao.rs.getString("codprod_epi"), conexao.rs.getString("nome_epi"), conexao.rs.getInt("quantidade_epi"), conexao.rs.getString("tamanho_epi")}));
            } while (conexao.rs.next());
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao preencher o arrayList" + ex);
            conexao.desconecta();
        }
        ModeloTabela modeloTabela = new ModeloTabela(dados, Colunas);
        tablePesquisa.setModel(modeloTabela);
        tablePesquisa.getColumnModel().getColumn(0).setPreferredWidth(70);
        tablePesquisa.getColumnModel().getColumn(0).setResizable(false);
        tablePesquisa.getColumnModel().getColumn(1).setPreferredWidth(200);
        tablePesquisa.getColumnModel().getColumn(1).setResizable(false);
        tablePesquisa.getColumnModel().getColumn(2).setPreferredWidth(80);
        tablePesquisa.getColumnModel().getColumn(2).setResizable(false);
        tablePesquisa.getColumnModel().getColumn(3).setPreferredWidth(80);
        tablePesquisa.getColumnModel().getColumn(3).setResizable(false);

        tablePesquisa.getTableHeader().setReorderingAllowed(false);
        tablePesquisa.setAutoResizeMode(tablePesquisa.AUTO_RESIZE_OFF);
        tablePesquisa.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        conexao.desconecta();
    }

    private Date converteData(String data) {
        Date d = null;
        try {
            data = data.substring(6) + "-"
                    + data.substring(3, 5) + "-"
                    + data.substring(0, 2);
            d = Date.valueOf(data);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return d;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Frm_CadSetor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Frm_CadSetor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Frm_CadSetor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Frm_CadSetor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Frm_EntregaEpi().setVisible(true);
            }
        });
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFinalizarEntrega;
    private javax.swing.JButton btnNovaEntrega;
    private javax.swing.JButton btnPEPI;
    private javax.swing.JButton btnPMatricula;
    private javax.swing.JButton btnPNome;
    private javax.swing.JFormattedTextField ftextData;
    private javax.swing.JFormattedTextField ftextHora;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JLabel lblData;
    private javax.swing.JLabel lblEntregaEpi;
    private javax.swing.JLabel lblHora;
    private javax.swing.JLabel lblIdEntrega;
    private javax.swing.JLabel lblMostraCodProd;
    private javax.swing.JLabel lblObservacoes;
    private javax.swing.JLabel lblPesquisaMatricula;
    private javax.swing.JLabel lblQuantidade;
    private javax.swing.JLabel lblResponsavel;
    private javax.swing.JTable tableItens;
    private javax.swing.JTable tablePesquisa;
    private javax.swing.JTextField textIdEntrega;
    private javax.swing.JTextPane textObservacoes;
    private javax.swing.JTextField textPesquisaEpi;
    private javax.swing.JTextField textPesquisaMatricula;
    private javax.swing.JTextField textPesquisaNome;
    private javax.swing.JTextField textQuantidade;
    private javax.swing.JTextField textResponsavel;
    // End of variables declaration//GEN-END:variables

}
